using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Unbound.Unbinders;

namespace Unbound.Configuration
{
	public class UnbindingOptions : IUnbindingOptions
	{
		string _prefix = string.Empty;
		object _source;
		SourceOptions _sourceOptions;

		public IUnbindingSourceOptions<T> Source<T>(T request)
		{
			_source = request;
			_sourceOptions = new SourceOptions<T>();
			return (SourceOptions<T>) _sourceOptions;
		}

		void IUnbindingOptions.Prefix(string prefix)
		{
			_prefix = prefix;
		}

		public IUnbindingRequest GetNewContext()
		{
			return new UnbindingContext(_source, _prefix, _sourceOptions.PropertiesToSkip);
		}

		class SourceOptions
		{
			protected readonly List<string> _propertiesToSkip = new List<string>();
			public IList<string> PropertiesToSkip { get { return _propertiesToSkip; } }
		}

		class SourceOptions<T> : SourceOptions, IUnbindingSourceOptions<T>
		{
			public IUnbindingSourceOptions<T> SkipProperty(Expression<Func<T, object>> propertyToSkip)
			{
				var name = ReflectionHelper.FindPropertyName(propertyToSkip);
				_propertiesToSkip.Add(name);
				return this;
			}

			public void SkipProperties(IEnumerable<Expression<Func<T, object>>> propertiesToSkip)
			{
				foreach (var skip in propertiesToSkip)
				{
					SkipProperty(skip);
				}
			}
		}
	}
}